package com.crm.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.cluePoolManager.ClueMapper;
import com.crm.dao.mapper.cluePoolManager.CluePoolPrivateMapper;
import com.crm.dao.mapper.customMapper.cluePoolManagerCustomMapper.CluePoolPrivateCustomMapper;
import com.crm.model.entity.cluePoolManager.ClueExample;
import com.crm.model.entity.cluePoolManager.CluePoolPrivate;
import com.crm.model.entity.cluePoolManager.CluePoolPrivateExample;
import com.crm.model.entity.cluePoolManager.CluePoolPublic;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.cluePoolManager.CluePoolPrivateQO;
import com.crm.redis.service.cluePoolManager.CluePoolPrivateRedisService;
import com.crm.service.cluePoolManager.CluePoolPrivateDbService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author yincuimei
 * @Date 2020/12/24 11:15
 */
@Service
public class CluePoolPrivateDbServiceImpl implements CluePoolPrivateDbService {

    @Autowired
    private CluePoolPrivateMapper cluePoolPrivateMapper;

    @Autowired
    private CluePoolPrivateCustomMapper cluePoolPrivateCustomMapper;

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private CluePoolPrivateRedisService cluePoolPrivateRedisService;

    @Override
    public Boolean insertCluePoolPrivateInfo(CluePoolPrivate cluePoolPrivate) {
        Boolean result = false;
        int num = cluePoolPrivateMapper.insertSelective(cluePoolPrivate);
        if (num > 0) {
            result = true;
        }
        return result;
    }


    /**
     * @author wll
     * @date 2021/02/01 11:30
     * info 用户模块-我的线索池-修改
     * @param cluePoolPrivateQO
     * @return
     */
    @Override
    public Boolean update(CluePoolPrivateQO cluePoolPrivateQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        cluePoolPrivateQO.setUpdatePeopleId(userAccount.getAccountId());
        cluePoolPrivateQO.setUpdateTm(new Date());
        Boolean result = false;
        int num = cluePoolPrivateCustomMapper.update(cluePoolPrivateQO);
        if (num > 0) {
            cluePoolPrivateRedisService.deleteCluePoolPrivateById(cluePoolPrivateQO.getCluePoolPrivateId());
            result = true;
        }
        return result;
    }

    /**
     * @author wll
     * @date 2021/02/01
     * info 用户模块-我的线索池-删除
     * @param id 线索池id
     * @return
     */
    @Override
    public Boolean delete(String id,String accountId, Date date) {
        Boolean result = false;
        int num = cluePoolPrivateCustomMapper.deleteClue( id,accountId,date);
        if (num > 0) {
            cluePoolPrivateRedisService.deleteCluePoolPrivateById(id);
            result = true;
        }
        return result;
    }


    /**
     * @author wll
     * @date 2020/12/29 10:00
     * info 用户模块-我的线索池
     * @param cluePoolPrivateInfo  搜索条件
     * @param sort 排序条件
     * @param sortOrder 正序/倒序
     * @return
     */
    @Override
    public List<CluePoolPrivate> selectCluePoolPrivateBySearchInfo(CluePoolPrivateQO cluePoolPrivateInfo, String sort, String sortOrder) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CluePoolPrivateExample example = new CluePoolPrivateExample();
        CluePoolPrivateExample.Criteria criteria = example.createCriteria();

        criteria.andCreatePeopleIdEqualTo(userAccount.getAccountId());
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));

        if(StringUtils.isNotEmpty(cluePoolPrivateInfo.getCluePoolName())){//线索池名称
            criteria.andCluePoolNameLike("%" + CommonUtils.escapeQueryChars(cluePoolPrivateInfo.getCluePoolName().trim()) + "%");
        }

        if(StringUtils.isNotEmpty(cluePoolPrivateInfo.getCityName())){//城市名称
            criteria.andCityNameLike("%" + CommonUtils.escapeQueryChars(cluePoolPrivateInfo.getCityName().trim()) + "%");
        }

        if(StringUtils.isNotEmpty(cluePoolPrivateInfo.getIndustry())){//行业
            criteria.andIndustryLike("%" + CommonUtils.escapeQueryChars(cluePoolPrivateInfo.getIndustry().trim()) + "%");
        }

        if(StringUtils.isNotEmpty(cluePoolPrivateInfo.getTag())){//标签
            criteria.andTagLike("%" + CommonUtils.escapeQueryChars(cluePoolPrivateInfo.getTag().trim()) + "%");
        }

        //拼凑表格排序内容
        String order = "default_mark desc";
        if(!StringUtils.isBlank(sort)&&!StringUtils.isBlank(sortOrder)){
            switch(sort){
                case "createTm":
                {
                    order= order + ",create_tm " + sortOrder;
                }
                break;
                case "cluePoolName":
                {
                    order= order + ",clue_pool_name " + sortOrder;
                }
                break;
                case "cityName":
                {
                    order= order + ",city_name " + sortOrder;
                }
                break;
                case "industry":
                {
                    order= order + ",industry " + sortOrder;
                }
                break;
                default:
                    break;
            }

        }

        example.setOrderByClause(order);
        List<CluePoolPrivate> cluePoolPrivates = cluePoolPrivateMapper.selectByExampleWithBLOBs(example);
        return cluePoolPrivates;
        }

        /**
         * @author wll
         * 判断线索池名称是否重复  添加 or修改
         * cluePoolPrivateId 线索池id
         */
      @Override
      public CluePoolPrivate selectCluePoolPrivateByName(String cluePoolName,String cluePoolPrivateId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        CluePoolPrivateExample example = new CluePoolPrivateExample();
        CluePoolPrivateExample.Criteria criteria = example.createCriteria();

        criteria.andCluePoolNameEqualTo(cluePoolName);
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));
        criteria.andCreatePeopleIdEqualTo(userAccount.getAccountId());
        if(StringUtils.isNotBlank(cluePoolPrivateId)){//修改线索池 判断名称是否重复
            criteria.andCluePoolPrivateIdNotEqualTo(cluePoolPrivateId);
        }
        List<CluePoolPrivate> cluePoolPrivateList = cluePoolPrivateMapper.selectByExample(example);
        if(cluePoolPrivateList.size()>0){
            return cluePoolPrivateList.get(0);
        }else {
            return null;
        }
    }

    /**
     * 根据登录人查询其默认池子的主键
     * @param createPeopleId    ---创建人ID
     * @param deleteStatus      ---删除状态(0:正常;1:已删除)
     * @return
     */
    @Override
    public String selectCluePoolPrivateId(String createPeopleId, Short deleteStatus) {
        return cluePoolPrivateCustomMapper.selectCluePoolPrivateId(createPeopleId,deleteStatus);
    }

    /**
     * @author wll
     *info 查询个人默认池容量 满了不能进行领取
     * return   true个人默认池容量未满    false个人默认池容量已满
     */
    @Override
    public JSONObject selectCluePoolPrivateDefaultCapacity(CluePoolPrivate cluePoolPrivate) {
        JSONObject result = new JSONObject();
        //查询个人默认池当前线索数
        ClueExample clueExample = new ClueExample();
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        ClueExample.Criteria clueCriteria = clueExample.createCriteria();
        clueCriteria.andClueStateIn(list);//1-已领取；2-已分配
        clueCriteria.andDeleteStatusEqualTo(Short.parseShort("0"));//删除状态---0: 正常; 1: 已删除
        clueCriteria.andCluePoolPrivateIdEqualTo(cluePoolPrivate.getCluePoolPrivateId());//线索池私有id
        long l = clueMapper.countByExample(clueExample);
        if(Integer.parseInt(String.valueOf(l)) >= cluePoolPrivate.getCapacity()){//个人默认池容量已满
            result.put("toast","个人默认池容量已满");
            result.put("status",false);
            return result;
        }else{
            result.put("status",true);
            return result;
        }


    }

    /**
     * @author wll
     * info 查询个人默认池信息
     */
    @Override
    public CluePoolPrivate selectCluePoolPrivate(UserAccount userAccount) {
        CluePoolPrivateExample example = new CluePoolPrivateExample();
        CluePoolPrivateExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteStatusEqualTo(Short.parseShort("0"));//删除状态---0: 正常; 1: 已删除
        criteria.andDefaultMarkEqualTo(1);//是否是默认池子：0-不是默认；1-默认池子
        criteria.andCreatePeopleIdEqualTo(userAccount.getAccountId());//创建人
        List<CluePoolPrivate> cluePoolPrivates = cluePoolPrivateMapper.selectByExample(example);
        if(cluePoolPrivates.size()>0){
            return cluePoolPrivates.get(0);
        }else {
            return null;
        }

    }

    @Override
    public CluePoolPrivate selectCluePoolPrivateById(String id) {
        //先在redis中查询私有线索池信息，若没有则在数据库中查询，再插入到redis中
        CluePoolPrivate cluePoolPrivate= cluePoolPrivateRedisService.getCluePoolPrivateById(id);
        if (cluePoolPrivate == null) {
            cluePoolPrivate = cluePoolPrivateMapper.selectByPrimaryKey(id);
            if (cluePoolPrivate != null) {
                cluePoolPrivateRedisService.setCluePoolPrivateById(cluePoolPrivate);
            }
        }
        return cluePoolPrivate;
    }



}